unit uFrmCameraGetImage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CAM, ExtCtrls, gnugettext, uCap, uImagePanel, ufrmsubjectimage;

type
  TFrmCameraGetImage = class(TForm)
    pnl_CamImage: TPanel;
    btn_TakePhoto: TButton;
    btn_Close: TButton;
    btn_SaveClose: TButton;
    Label1: TLabel;
    Label2: TLabel;
    pnl_Horiz: TPanel;
    pnl_Verti: TPanel;
    btn_Settings: TButton;
    pnl_TakkenPhoto: TPanel;
    procedure btn_TakePhotoClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btn_SaveCloseClick(Sender: TObject);
    procedure btn_CloseClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure btn_SettingsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    m_SubjectID: string;
    { Private declarations }
    procedure CloseCamHandle();
    Function PanelToBmp(Panel: TPanel): TBitmap;
  public
    { Public declarations }
    FrmSubjectImage: TFrmSubjectImage;
    ImageValid:boolean;
    procedure ShowForm(SubjectID: string);
  end;

var
  FrmCameraGetImage: TFrmCameraGetImage;

implementation

uses uFrmMain, uUtils;
{$R *.dfm}

procedure TFrmCameraGetImage.btn_CloseClick(Sender: TObject);
begin
  CloseCamHandle;
  ImageValid := false;
  close();
end;

procedure TFrmCameraGetImage.btn_SaveCloseClick(Sender: TObject);
begin
  FrmMain.SavePhotoToDB('PhotoFromCamera.JPG', 'JPG', m_SubjectID);
  DeleteFile('PhotoFromCamera.JPG');
  CloseCamHandle;
  ImageValid := true;
  close();
end;

procedure TFrmCameraGetImage.btn_SettingsClick(Sender: TObject);
begin
  SendMessage(CAMHandle, WM_CAP_DLG_VIDEOSOURCE, 0, 0);
end;

procedure TFrmCameraGetImage.ShowForm(SubjectID: string);
begin
  m_SubjectID := SubjectID;
  FrmSubjectImage.img_TakenPhoto.Picture := nil;
  CreateCAMPanel(pnl_CamImage.Handle, 240, 320, 0);
  // ConnectCam();
  ShowModal();
end;

procedure TFrmCameraGetImage.btn_TakePhotoClick(Sender: TObject);
var
  ABitmap: TBitmap;
begin
  pnl_Horiz.Visible := false;
  pnl_Verti.Visible := false;
  pnl_CamImage.Repaint;
  ABitmap := TBitmap.Create();
  ABitmap.Height := 240;
  ABitmap.Width := 320;
  // CameraShot(ABitmap);
  ABitmap := PanelToBmp(pnl_CamImage);
  pnl_Horiz.Visible := true;
  pnl_Verti.Visible := true;
  FrmSubjectImage.img_TakenPhoto.Picture.Assign(ABitmap);
  uUtils.BMPtoJPG(ABitmap, 'PhotoFromCamera.JPG');
end;

Function TFrmCameraGetImage.PanelToBmp(Panel: TPanel): TBitmap;
VAR
  bmp: TBitmap;
  DC: HDC;
Begin
  bmp := TBitmap.Create;
  bmp.Width := Panel.Width;
  bmp.Height := Panel.Height;
  DC := GetDc(Panel.Handle);
  Bitblt(bmp.canvas.Handle, 0, 0, Panel.Width, Panel.Height, DC, 0, 0, srccopy);
  Releasedc(Panel.Handle, DC);
  result := bmp;
End;

procedure TFrmCameraGetImage.CloseCamHandle;
begin
  try
    DisconnectCam();
    Releasedc(0, CAMHandle);
  except
    on E: Exception do
    begin
      // nothing
    end;
  end;
end;

procedure TFrmCameraGetImage.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  CloseCamHandle();
end;

procedure TFrmCameraGetImage.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
  FrmSubjectImage := TFrmSubjectImage.Create(self);
  FrmSubjectImage.Parent := pnl_TakkenPhoto;
  FrmSubjectImage.Align := alClient;
  FrmSubjectImage.Show;
  pnl_Verti.Visible := true;
end;

procedure TFrmCameraGetImage.FormDestroy(Sender: TObject);
begin
  CloseCamHandle;
end;

procedure TFrmCameraGetImage.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
  begin
    CloseCamHandle;
    close;
  end;
end;

end.
